/**
 * 
 */
package com.shop.cloud.Scheduled;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;

import com.shop.cloud.JPush.MsgPushUtils;
import com.shop.cloud.domain.hongfu.Banner;
import com.shop.cloud.domain.hongfu.Message;
import com.shop.cloud.domain.hongfu.User;
import com.shop.cloud.service.hongfu.IMessageService;
import com.shop.cloud.service.hongfu.impl.BannerService;
import com.shop.cloud.service.hongfu.impl.UserService;

/**
 * @author 作者姓名 E-mail: email地址
 * @version 创建时间：2018年1月10日 上午10:27:34
 * 类说明
 */
/**
 * @author Administrator
 *
 */
@Component
public class ScheduledController {

	@Autowired
	private IMessageService messageService;

	@Autowired
	private UserService userService;
	
	@Autowired
	private BannerService bannerService;

	/*
	 * @Scheduled(cron="0/5 * *  * * ? ") //每5秒执行一次 public void myTest(){
	 * System.out.println("进入测试"); }
	 */

//	@Scheduled(cron = "0/5 * *  * * ? ")
//	public void run() {
//
//		System.out.println("-------------------------    ");
//
//	}

	//十分钟
	@Scheduled(fixedRate = 600000)
	public void testTasks() {
		//消息定时推送
		System.out.println("开始定时任务：推送消息");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("type", 1);
		params.put("isPush", 0);
		List<Message> messList = messageService.find(params);
		for (Message message : messList) {
			long current = System.currentTimeMillis();
			 //十一分钟
		    long current2 = current + 660000;
			if (message.getDisplayTime()!=null&&current<message.getDisplayTime().getTime() && message.getDisplayTime().getTime()<current2 ) {

				// 极光推送消息:全部
				if (message.getPushType() == 0) {
					MsgPushUtils.buildPushObject_all_all_alert(message.getContent());
					message.setIsPush(1);
					messageService.update(message);
				}
				// 极光推送：门店
				if (message.getPushType() == 1) {
					
					try {
						User user = new User();
						user.setRoleId(message.getPushId());
						List<User> uList = userService.find(user);
						for (User user2 : uList) {
							MsgPushUtils.addJPush(message.getContent(), user2.getId().toString());
							message.setIsPush(1);
							messageService.update(message);
						}
					} catch (APIConnectionException e) {
						e.printStackTrace();
					} catch (APIRequestException e) {
						e.printStackTrace();
					}

				}
			}
		}
		
		//banner图片定时下架
		System.out.println("开始定时任务：banner图片定时下架");
		Banner banner = new Banner();
		banner.setIsDisplay(0);
		List<Banner> bannerlist =bannerService.find(banner);
		for (Banner banner2 : bannerlist) {
			long current = System.currentTimeMillis();
		    if (current > banner2.getSoldoutTime().getTime()) {
		    	//修改上架状态 1-下架
		    	banner.setIsDisplay(1);
		    	bannerService.update(banner);
		    }
		}
		
	}

	public static void main(String[] args) {
	}

}
